﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace MineSweeperBot
{
    class PixelPharser
    {
        #region Imports

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(String sClassName, String sAppName);

        // Activate an application window.
        [DllImport("USER32.DLL")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [StructLayout(LayoutKind.Sequential)]
        struct WINDOWINFO
        {
            public uint cbSize;
            public RECT rcWindow;
            public RECT rcClient;
            public uint dwStyle;
            public uint dwExStyle;
            public uint dwWindowStatus;
            public uint cxWindowBorders;
            public uint cyWindowBorders;
            public ushort atomWindowType;
            public ushort wCreatorVersion;

            public WINDOWINFO(Boolean? filler)
                : this()   // Allows automatic initialization of "cbSize" with "new WINDOWINFO(null/true/false)".
            {
                cbSize = (UInt32)(Marshal.SizeOf(typeof(WINDOWINFO)));
            }

        }

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02; 
        private const int MOUSEEVENTF_LEFTUP = 0x04; 
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08; 
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;        // x position of upper-left corner
            public int Top;         // y position of upper-left corner
            public int Right;       // x position of lower-right corner
            public int Bottom;      // y position of lower-right corner
        }

        #endregion

        private static Pixel[,] PixelArray = new Pixel[9,9];
        ScreenShot.ScreenCapture SC = new ScreenShot.ScreenCapture();

        public static void ClickTile(IntPtr Handle)
        {
            
            if (NeedRandomClick())
            {
                Random randomTileGen = new Random();
                Pixel randomPixel = PixelArray[randomTileGen.Next(0, 8), randomTileGen.Next(0, 8)];
                IntPtr hWND = FindWindow(null, "MineSweeper");
                RECT rec = new RECT();
                GetWindowRect(Handle, out rec);
                SetForegroundWindow(Handle);

                DoMouseEvent(Handle, randomPixel.XPostion, randomPixel.YPostion);
            }
        }
        public static void DoMouseEvent(IntPtr Handle, int PosX, int PosY)
        {
            Cursor.Position = new Point(PosX, PosY);
            Rectangle _rectangle = new Rectangle();
            RECT rct;
            if (Handle != IntPtr.Zero)
            {
                if (!GetWindowRect(Handle, out rct))
                {
                    MessageBox.Show("ERROR");
                    return;
                }

                Console.WriteLine(rct);

                _rectangle.X = rct.Left;
                _rectangle.Y = rct.Right;
                _rectangle.Width = rct.Right - rct.Left;
                _rectangle.Height = rct.Bottom - rct.Top;
            }

            int x = _rectangle.X;
            int y = _rectangle.Y;
            Cursor.Position = new Point(x, y);
            SetForegroundWindow(Handle);
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);

            ///SetForegroundWindow(Handle);
            //SendInput.SendInputClass.Click();
        }
        public static bool NeedRandomClick()
        {
           //Needs some testing logic!
           /* foreach (Pixel pix in PixelArray)
            {
                if (pix.PixelColor !=  Color.("ffc0c0c0"))
                {
                    return false;
                }
            }*/
            return true;
        }
        public static void FillPixelArray(Image img)
        {
            Pixel TempPixel = new Pixel();
            int x = 22;
            int y = 111;

            for (int i = 0; i <= 8; i++)
            {
                for (int f = 0; f <= 8; f++)
                {
                    TempPixel.PixelColor = GetPixelColor(img, x, y);
                    TempPixel.XPostion = x;
                    TempPixel.YPostion = y;

                    PixelArray[i, f] = TempPixel;
                    y += 16;
                }
                x += 16;
                y = 111;
            }
        }
        public static Color GetPixelColor(Image img, int x, int y)
        {
            if (x > 0 && y > 0)
            {
                Bitmap bitmap = new Bitmap(img);
                Color pixelColor = bitmap.GetPixel(x, y);
                return pixelColor;
            }
            return Color.DarkBlue;
        }
    }
}
